package com.hs.android.fortress.ingame;

import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMapOptions;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.MapView;
import com.google.android.gms.maps.GoogleMap.OnCameraChangeListener;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.hs.android.fortress.GameApplication;
import com.hs.android.fortress.R;
import com.hs.android.fortress.R.id;
import com.hs.android.fortress.R.layout;
import com.hs.android.fortress.ingame.GameInformationGetter.Listener;
import com.hs.android.fortress.intro.WaitingRoomActivitiy;
import com.hs.android.fortress.model.Bomb;
import com.hs.android.fortress.model.ExplosionInformation;
import com.hs.android.fortress.model.Player;
import com.hs.android.fortress.model.Room;
import com.hs.android.fortress.model.ShootingInfomation;
import com.hs.android.fortress.net.AsyncNetworking;
import com.hs.android.fortress.net.FormPostCommand;
import com.hs.android.fortress.net.ParamsMaker;
import com.hs.android.fortress.net.PositionService;
import com.hs.android.fortress.net.AsyncNetworking.onNetworkProcessListener;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.KeyEvent;

public class GameScreenActivity extends AbstractGameActivity {

	private final static String TAG = GameScreenActivity.class.getSimpleName();
	
	public static final String EXTRA_ROOM_NUMBER = "com.hs.android.fortress.ingame.room_number";
	
	private GameApplication app;
	
	private MenuScreenFragment mMenuFragment;
	private MainScreenFragment mMainFragment;
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		mMenuFragment.setPowerListener(mPowerListener);
	}

	@Override
	protected String setUserId() {
		app = (GameApplication)getApplication();
		return app.getUserId();
	}


	@Override
	protected int setRoomNumber() {
		return getIntent().getIntExtra(EXTRA_ROOM_NUMBER, 0);
	}


	@Override
	protected AbstractMainScreenFragment createMainScreenFragment() {
		mMainFragment = new MainScreenFragment();
		return mMainFragment;
	}


	@Override
	protected AbstractMenuScreenFragment createMenuScreenFragment() {
		mMenuFragment = new MenuScreenFragment();
		return mMenuFragment;
	}


	@Override
	protected void setPlayer(Player myPlayer) {
		app.setMyPlayer(myPlayer);
	}
	
	@Override
	protected void confirmWinning(){
		if(mMainFragment.isFinish()){
			mGetter.setListener(null);
			
			new AlertDialog.Builder(GameScreenActivity.this)
				.setIcon(android.R.drawable.ic_dialog_alert)
				.setTitle("Win")
				.setMessage("You Win !")
				.setPositiveButton("OK",new DialogInterface.OnClickListener() {
							
					@Override
					public void onClick(DialogInterface dialog,	int which) {
		
						AsyncNetworking.newInstance(GameScreenActivity.this,new FormPostCommand("/gamecontroller/confirm",
							ParamsMaker.makeRoomNumberParam(mRoomNumber)),
								new onNetworkProcessListener() {
											@Override
											public void onPostExecute(String result) {
												if(result != null)
													GameScreenActivity.this.finish();
											}
								}).execute();
							}
						}).show();
		}
	}
	
	@Override
	protected void Lose(){
			mGetter.setListener(null);
			
			new AlertDialog.Builder(GameScreenActivity.this)
			.setIcon(android.R.drawable.ic_dialog_alert)
			.setTitle("Lose")
			.setMessage("You Lose....")
			.setPositiveButton("OK",new DialogInterface.OnClickListener() {
						
				@Override
				public void onClick(DialogInterface dialog,	int which) {
					GameScreenActivity.this.finish();
				}
			}).show();
		
	}
	
	@Override
	protected void draw() {
			mGetter.setListener(null);
			
			new AlertDialog.Builder(GameScreenActivity.this)
			.setIcon(android.R.drawable.ic_dialog_alert)
			.setTitle("Draw")
			.setMessage("Draw...")
			.setPositiveButton("OK",new DialogInterface.OnClickListener() {
						
				@Override
				public void onClick(DialogInterface dialog,	int which) {
					GameScreenActivity.this.finish();
				}
			}).show();
		
	}
	
	@Override
	protected void onResume() {
		super.onResume();

		setGetterListener(mListener);
		mMenuFragment.setDirListener(mDirListener);
	};

	onDirectionChangeListener mDirListener = new onDirectionChangeListener() {

		@Override
		public void onDirectionChanged(float degree) {
			mMainFragment.screenRotate(degree);
		}
	};

	onPowerPushlListener mPowerListener = new onPowerPushlListener() {

		@Override
		public void btnPowerClick(LatLng exPos,Bomb bomb, float angle) {
			mMainFragment.drawExplosion(exPos, bomb, angle);
			
		}
	};
	
	public boolean onKeyDown(int keyCode, android.view.KeyEvent event) {

		if (keyCode == KeyEvent.KEYCODE_BACK) {
			mGetter.setListener(null);
			
			new AlertDialog.Builder(this)
					.setIcon(android.R.drawable.ic_dialog_alert)
					.setTitle("Give Up")
					.setMessage("Do you want to Give Up?")
					.setPositiveButton("Yes",new OnClickListener() {
								
						@Override
						public void onClick(DialogInterface dialog,	int which) {
							
							String userId = ((GameApplication) getApplication()).getUserId();

							AsyncNetworking.newInstance(GameScreenActivity.this,new FormPostCommand("/gamecontroller/giveup",
								ParamsMaker.makeIdNumberParams(mUserId, mRoomNumber)),null).execute();
								
							GameScreenActivity.this.finish();
						}
					}).setNegativeButton("No",	new OnClickListener() {
										
						@Override
						public void onClick(DialogInterface dialog, int which) {
							mGetter.setListener(mListener);
						}
					}).show();
			
			return true;
		}

		return super.onKeyDown(keyCode, event);
	};
	
	
	private Listener mListener = new Listener() {

		@Override
		public void onPositionsGotten(List<Player> players) {
			setGottenInformation(players);
		}

		@Override
		public void onExposionGotten(List<ExplosionInformation> explosionInformations) {
			DrawEnermyExplostion(explosionInformations);
		}
	};

}
